package myfamily.model;
import java.util.*;
public class Marriage {


	private GenericPerson participant1,participant2; 
	private boolean dissolved;
	private ArrayList<GenericPerson> childList;
	private Date dateOfMarriage;
	private Date dateOfDissolution;


	/**
	 * 
	 */
	public Marriage() {
	}


	public Marriage(GenericPerson p1, GenericPerson p2, boolean dslvd, Date dom, Date dod)
	{	this.childList = new ArrayList<GenericPerson>();
		this.participant1 = p1;
		this.participant2 = p2;
		this.dateOfDissolution = dod;
		this.dateOfMarriage = dom;
		this.dissolved = dslvd;
		p1.addMarriage(this);
		if(p2 != null )
			p2.addMarriage(this);
	}
	
	public void persist()
	{
		DatabaseObject db = DatabaseObjectFactory.getDatabaseObject();
		db.pm.currentTransaction().begin();
		db.pm.makePersistent(this);
		db.pm.currentTransaction().commit();
	}

	/**
	 * @return the dissolved
	 */
	public boolean isDissolved() {
		return dissolved;
	}
	/**
	 * @param dissolved the dissolved to set
	 */
	public void setDissolved(boolean dissolved) {
		DatabaseObject db = DatabaseObjectFactory.getDatabaseObject();
		db.pm.currentTransaction().begin();
		this.dissolved = dissolved;
		db.pm.currentTransaction().commit();
	}
	/**
	 * @return the childList
	 */
	public ArrayList<GenericPerson> getChildList() {
		return childList;
	}
	/**
	 * @param childList the childList to set
	 */
	public void addChild(GenericPerson child)
	{
		DatabaseObject db = DatabaseObjectFactory.getDatabaseObject();
		db.pm.currentTransaction().begin();		
		this.childList.add(child);
		db.pm.currentTransaction().commit();
		child.setChildOf(this);
	}
	/**
	 * @return the dateOfMarriage
	 */
	public Date getDateOfMarriage() {
		return dateOfMarriage;
	}
	/**
	 * @param dateOfMarriage the dateOfMarriage to set
	 */
	public void setDateOfMarriage(Date dateOfMarriage) {
		DatabaseObject db = DatabaseObjectFactory.getDatabaseObject();
		db.pm.currentTransaction().begin();
		this.dateOfMarriage = dateOfMarriage;
		db.pm.currentTransaction().commit();
	}
	/**
	 * @return the dateOfDissolution
	 */
	public Date getDateOfDissolution() {
		return dateOfDissolution;
	}
	/**
	 * @param dateOfDissolution the dateOfDissolution to set
	 */
	public void setDateOfDissolution(Date dateOfDissolution) {
		DatabaseObject db = DatabaseObjectFactory.getDatabaseObject();
		db.pm.currentTransaction().begin();
		this.dateOfDissolution = dateOfDissolution;
		db.pm.currentTransaction().commit();
	}

	/**
	 * @param spouse the spouse to set
	 */
	public void setSpouse(GenericPerson spouse) {
		DatabaseObject db = DatabaseObjectFactory.getDatabaseObject();
		db.pm.currentTransaction().begin();
		participant1 = spouse;
		db.pm.currentTransaction().commit();
	}

	/**
	 * @return the spouse
	 */
	public GenericPerson getSpouse() {
		return participant1;
	}

	/**
	 * @return the participant1
	 */
	public GenericPerson getParticipant1() {
		return participant1;
	}

	/**
	 * @param participant1 the participant1 to set
	 */
	public void setParticipant1(GenericPerson participant1) {
		DatabaseObject db = DatabaseObjectFactory.getDatabaseObject();
		db.pm.currentTransaction().begin();
		this.participant1 = participant1;
		db.pm.currentTransaction().commit();
	}

	/**
	 * @return the participant2
	 */
	public GenericPerson getParticipant2() {
		return participant2;
	}

	/**
	 * @param participant2 the participant2 to set
	 */
	public void setParticipant2(GenericPerson participant2) {
		DatabaseObject db = DatabaseObjectFactory.getDatabaseObject();
		db.pm.currentTransaction().begin();
		this.participant2 = participant2;
		db.pm.currentTransaction().commit();
	}


	public boolean isSameAs(Marriage m)
	{
		if(this.participant1.isSameAs(m.participant1) && this.participant2.isSameAs(m.participant2))
			return true;
		if(this.participant1.isSameAs(m.participant2) && this.participant2.isSameAs(m.participant1))
			return true;

		return false;
	}

/*	public void merge(Marriage m)
	{
		if(m == null) return;
		
		if(m == this) return;
		
		if( this.isSameAs(m) == false ) return;

		if(this.participant1.isSameAs(m.participant1))
		{
			if(this.participant1.isRegistered())
			{
				this.participant1.merge(m.participant1,false,true);
			}
			else if(m.participant1.isRegistered())
			{
				m.participant1.merge(participant1,false,true);
				this.participant1 = m.participant1;		//to transfer the registered user to the calling object 
			}
			else
			{
				this.participant1.merge(m.participant1,false,true);
			}

			if(this.participant2.isRegistered())
			{
				this.participant2.merge(m.participant2,false,true);
			}
			else if(m.participant2.isRegistered())
			{
				m.participant2.merge(participant2,false,true);
				this.participant2 = m.participant2;	//to transfer the registered user to the calling object 
			}
			else
			{
				this.participant2.merge(m.participant2,false,true);
			}
		}
		else if(this.participant1.isSameAs(m.participant2))
		{
			if(this.participant1.isRegistered())
			{
				this.participant1.merge(m.participant2,false,true);
			}
			else if(m.participant2.isRegistered())
			{
				m.participant2.merge(participant1,false,true);
				this.participant2 = m.participant1;	//to transfer the registered user to the calling object 
			}
			else
			{
				this.participant1.merge(m.participant2,false,true);
			}

			if(this.participant2.isRegistered())
			{
				this.participant2.merge(m.participant1,false,true);
			}
			else if(m.participant1.isRegistered())
			{
				m.participant1.merge(participant2,false,true);
				this.participant2 = m.participant1;	//to transfer the registered user to the calling object 
			}
			else
			{
				this.participant2.merge(m.participant1,false,true);
			}
		}

		for(GenericPerson child : m.childList)
			this.childList.add(child);

		for( int i = 0 ; i < childList.size() ; i++ )
			for( int j = i + 1 ; j < childList.size() ; j++ )
			{
				if(this.childList.get(i).isSameAs(this.childList.get(j)))
					this.childList.get(i).merge(this.childList.get(j),true,false);
				if(this.childList.get(j).isRegistered())
				{
					this.childList.remove(i);
					break;
				}
				else
					this.childList.remove(j);
			}
	}	*/

}
